﻿using AutoMapper;
using QMERP.Domain.Dtos;
using QMERP.Domain.Entities;
using QMERP.Domain.Enums;
using QMERP.Domain.ImportModels;
using QMERP.Infrastructure.Extensions;
using QMERP.Infrastructure.Tools;

namespace QMERP.Domain.AutoMapper
{
    public class DomainToDtoMappingProfile : Profile
    {
        /// <summary>
        /// 配置构造函数，用来创建关系映射
        /// </summary>
        public DomainToDtoMappingProfile()
        {
            #region App

            CreateMap<App, AppResponse>();
            CreateMap<App, RouterMetaResponse>();
            CreateMap<App, RouterResponse>()
                .ForMember(
                    dto => dto.Meta,
                    domain => domain.MapFrom(src => src)
                );

            #endregion

            #region Role
            CreateMap<Role, RoleResponse>();
            #endregion

            #region Privilege
            CreateMap<Privilege, PrivilegeResponse>();
            #endregion

            #region User
            CreateMap<User, UserResponse>()
                .ForMember(
                   dto => dto.OrganizationIds,
                   domain => domain.MapFrom(src => src.User_Organizations == null ? null : src.User_Organizations.Select(x => x.OrganizationId))
                )
                .ForMember(
                   dto => dto.OrganizationNames,
                   domain => domain.MapFrom(src => src.User_Organizations == null ? null : src.User_Organizations.Select(x => x.Organization!.Name))
                )
                .ForMember(
                   dto => dto.RoleIds,
                   domain => domain.MapFrom(src => src.User_Roles == null ? null : src.User_Roles.Select(x => x.RoleId))
                ).ForMember(
               dto => dto.RoleNames,
               domain => domain.MapFrom(src => src.User_Roles!.Select(s => s.Role!.Name))
            );
            CreateMap<User, CurrentUserResponse>().ForMember(
               dto => dto.OrganizationIds,
               domain => domain.MapFrom(src => src.User_Organizations == null ? null : src.User_Organizations.Select(x => x.OrganizationId))
            );
            #endregion

            #region Organization
            CreateMap<Organization, OrganizationResponse>();
            #endregion

            #region Supplier

            CreateMap<Supplier, SupplierResponse>();

            #endregion

            #region Currency

            CreateMap<Currency, CurrencyResponse>();

            #endregion

            #region LogOperate
            CreateMap<LogOperate, LogOperateResponse>();
            #endregion

            #region LogOperate

            CreateMap<LogSignIn, LogSignInResponse>();

            #endregion

            #region Dictionary
            CreateMap<Dictionary, DictionaryResponse>();
            #endregion

            #region DictionaryCategory
            CreateMap<DictionaryCategory, DictionaryCategoryResponse>();
            #endregion

            #region Lens

            CreateMap<Lens, LensResponse>().ForMember(
                   dto => dto.SupplierIds,
                   domain => domain.MapFrom(src => src.Lens_Suppliers!.Select(s => s.SupplierId))
                ).ForMember(
                   dto => dto.Currency,
                   domain => domain.MapFrom(src => src.Currency)
                );

            #endregion

            #region SalesProcess

            CreateMap<SalesProcess, SalesProcessResponse>();

            #endregion

            #region Warehouse

            CreateMap<Warehouse, WarehouseResponse>();

            #endregion

            #region Stock

            CreateMap<Stock, StockResponse>()
                .ForMember(
                   dto => dto.LensDetails,
                   domain => domain.MapFrom(src => src.StockDetails)
                ).ForMember(
                   dto => dto.BasicQtyLensDetails,
                   domain => domain.MapFrom(src => src.StockDetails)
                )
                .ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.StockDetails.Sum(x => x.Qty))
                ).ForMember(
                   dto => dto.BasicQty,
                   domain => domain.MapFrom(src => src.StockDetails.Sum(x => x.BasicQty))
                ).ForMember(
                   dto => dto.Warehouse,
                   domain => domain.MapFrom(src => src.Warehouse)
                ).ForMember(
                   dto => dto.Lens,
                   domain => domain.MapFrom(src => src.Lens)
                );

            CreateMap<Stock, LensViewResponse>()
                .ForMember(
                  dto => dto.Warehouse,
                  domain => domain.MapFrom(src => src.Warehouse)
               ).ForMember(
                 dto => dto.Lens,
                  domain => domain.MapFrom(src => src.Lens)
               ).ForMember(
                 dto => dto.LensDetails,
                  domain => domain.MapFrom(src => src.StockDetails)
               );

            CreateMap<StockDetail, LensDetailResponse>();


            #endregion

            #region StockActionLog

            CreateMap<StockActionLog, StockActionLogResponse>()
                .ForMember(
                   dto => dto.LensDetails,
                   domain => domain.MapFrom(src => src.StockActionLogDetails)
                ).ForMember(
                   dto => dto.Warehouse,
                   domain => domain.MapFrom(src => src.Warehouse)
                ).ForMember(
                  dto => dto.Lens,
                   domain => domain.MapFrom(src => src.Lens)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.StockActionLogDetails.Sum(x => x.Qty))
                ).ForMember(
                   dto => dto.StockQty,
                   domain => domain.MapFrom(src => src.StockActionLogDetails.Sum(x => x.StockQty))
                );
            CreateMap<StockActionLog, StockActionLogMainInfoResponse>()
                .ForMember(
                   dto => dto.LensDetails,
                   domain => domain.MapFrom(src => src.StockActionLogDetails)
                ).ForMember(
                   dto => dto.StockActionType,
                   domain => domain.MapFrom(src => src.StockActionType.GetDescription<StockActionTypeEnum>())
                ).ForMember(
                   dto => dto.BusinessType,
                   domain => domain.MapFrom(src => src.StockActionType == StockActionTypeEnum.EnterStock.ToString() ? src.BusinessType.GetDescription<EnterStockBusinessTypeEnum>() : src.BusinessType.GetDescription<LeaveStockBusinessTypeEnum>())
                ).ForMember(
                   dto => dto.Warehouse,
                   domain => domain.MapFrom(src => src.Warehouse)
                ).ForMember(
                  dto => dto.Lens,
                   domain => domain.MapFrom(src => src.Lens)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.StockActionLogDetails.Sum(x => x.Qty))
                ).ForMember(
                   dto => dto.StockQty,
                   domain => domain.MapFrom(src => src.StockActionLogDetails.Sum(x => x.StockQty))
                );


            CreateMap<StockActionLogDetail, LensDetailResponse>();

            #endregion

            #region EnterStockOrder

            CreateMap<EnterStockOrder, EnterStockOrderResponse>()
                .ForMember(
                   dto => dto.EnterStockOrderDetails,
                   domain => domain.MapFrom(src => src.EnterStockOrderDetails)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.EnterStockOrderDetails.SelectMany(s => s.EnterStockOrderDetail_LensDetails).Sum(s => s.Qty))
                );

            CreateMap<EnterStockOrderDetail, EnterStockOrderDetailResponse>()
                .ForMember(
                   dto => dto.LensDetails,
                   domain => domain.MapFrom(src => src.EnterStockOrderDetail_LensDetails)
                ).ForMember(
                   dto => dto.Warehouse,
                   domain => domain.MapFrom(src => src.Warehouse)
                ).ForMember(
                  dto => dto.Lens,
                   domain => domain.MapFrom(src => src.Lens)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.EnterStockOrderDetail_LensDetails.Sum(x => x.Qty))
                );

            CreateMap<EnterStockOrderDetail, EnterStockOrderMainInfoResponse>()
                .ForMember(
                   dto => dto.LensDetails,
                   domain => domain.MapFrom(src => src.EnterStockOrderDetail_LensDetails)
                ).ForMember(
                   dto => dto.Number,
                   domain => domain.MapFrom(src => src.EnterStockOrder!.Number)
                ).ForMember(
                   dto => dto.BusinessOrderNumber,
                   domain => domain.MapFrom(src => src.EnterStockOrder!.BusinessOrderNumber)
                ).ForMember(
                   dto => dto.BusinessOrderType,
                   domain => domain.MapFrom(src => src.EnterStockOrder!.BusinessOrderType.GetDescription<EnterStockBusinessOrderTypeEnum>())
                ).ForMember(
                   dto => dto.BusinessType,
                   domain => domain.MapFrom(src => src.EnterStockOrder!.BusinessType.GetDescription<EnterStockBusinessTypeEnum>())
                ).ForMember(
                   dto => dto.Status,
                   domain => domain.MapFrom(src => src.EnterStockOrder!.Status.GetDescription<EnterStockOrderStatusEnum>())
                ).ForMember(
                   dto => dto.CreatedBy,
                   domain => domain.MapFrom(src => src.EnterStockOrder!.CreatedBy)
                ).ForMember(
                   dto => dto.CreatedByName,
                   domain => domain.MapFrom(src => src.EnterStockOrder!.CreatedByName)
                ).ForMember(
                   dto => dto.CreatedOn,
                   domain => domain.MapFrom(src => src.EnterStockOrder!.CreatedOn)
                ).ForMember(
                   dto => dto.UpdatedBy,
                   domain => domain.MapFrom(src => src.EnterStockOrder!.UpdatedBy)
                ).ForMember(
                   dto => dto.UpdatedByName,
                   domain => domain.MapFrom(src => src.EnterStockOrder!.UpdatedByName)
                ).ForMember(
                   dto => dto.UpdatedOn,
                   domain => domain.MapFrom(src => src.EnterStockOrder!.UpdatedOn)
                ).ForMember(
                   dto => dto.CheckedBy,
                   domain => domain.MapFrom(src => src.EnterStockOrder!.CheckedBy)
                ).ForMember(
                   dto => dto.CheckedByName,
                   domain => domain.MapFrom(src => src.EnterStockOrder!.CheckedByName)
                ).ForMember(
                   dto => dto.CheckedOn,
                   domain => domain.MapFrom(src => src.EnterStockOrder!.CheckedOn)
                ).ForMember(
                   dto => dto.Warehouse,
                   domain => domain.MapFrom(src => src.Warehouse)
                ).ForMember(
                   dto => dto.Lens,
                   domain => domain.MapFrom(src => src.Lens)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.EnterStockOrderDetail_LensDetails.Sum(s => s.Qty))
                );

            CreateMap<EnterStockOrderDetail_LensDetail, LensDetailResponse>();

            #endregion

            #region LeaveStockOrder

            CreateMap<LeaveStockOrder, LeaveStockOrderResponse>()
                .ForMember(
                   dto => dto.LeaveStockOrderDetails,
                   domain => domain.MapFrom(src => src.LeaveStockOrderDetails)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.LeaveStockOrderDetails.SelectMany(s => s.LeaveStockOrderDetail_LensDetails).Sum(s => s.Qty))
                );

            CreateMap<LeaveStockOrderDetail, LeaveStockOrderDetailResponse>()
                .ForMember(
                   dto => dto.LensDetails,
                   domain => domain.MapFrom(src => src.LeaveStockOrderDetail_LensDetails)
                ).ForMember(
                   dto => dto.Warehouse,
                   domain => domain.MapFrom(src => src.Warehouse)
                ).ForMember(
                  dto => dto.Lens,
                   domain => domain.MapFrom(src => src.Lens)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.LeaveStockOrderDetail_LensDetails.Sum(x => x.Qty))
                );

            CreateMap<LeaveStockOrderDetail_LensDetail, LensDetailResponse>();

            CreateMap<LeaveStockOrderDetail, LeaveStockOrderMainInfoResponse>()
                .ForMember(
                   dto => dto.LensDetails,
                   domain => domain.MapFrom(src => src.LeaveStockOrderDetail_LensDetails)
                ).ForMember(
                   dto => dto.Number,
                   domain => domain.MapFrom(src => src.LeaveStockOrder!.Number)
                ).ForMember(
                   dto => dto.BusinessOrderNumber,
                   domain => domain.MapFrom(src => src.LeaveStockOrder!.BusinessOrderNumber)
                ).ForMember(
                   dto => dto.BusinessOrderType,
                   domain => domain.MapFrom(src => src.LeaveStockOrder!.BusinessOrderType.GetDescription<LeaveStockBusinessOrderTypeEnum>())
                ).ForMember(
                   dto => dto.BusinessType,
                   domain => domain.MapFrom(src => src.LeaveStockOrder!.BusinessType.GetDescription<LeaveStockBusinessTypeEnum>())
                ).ForMember(
                   dto => dto.LeaveStockWay,
                   domain => domain.MapFrom(src => src.LeaveStockOrder!.LeaveStockWay)
                ).ForMember(
                   dto => dto.Status,
                   domain => domain.MapFrom(src => src.LeaveStockOrder!.Status.GetDescription<LeaveStockOrderStatusEnum>())
                ).ForMember(
                   dto => dto.CreatedBy,
                   domain => domain.MapFrom(src => src.LeaveStockOrder!.CreatedBy)
                ).ForMember(
                   dto => dto.CreatedByName,
                   domain => domain.MapFrom(src => src.LeaveStockOrder!.CreatedByName)
                ).ForMember(
                   dto => dto.CreatedOn,
                   domain => domain.MapFrom(src => src.LeaveStockOrder!.CreatedOn)
                ).ForMember(
                   dto => dto.UpdatedBy,
                   domain => domain.MapFrom(src => src.LeaveStockOrder!.UpdatedBy)
                ).ForMember(
                   dto => dto.UpdatedByName,
                   domain => domain.MapFrom(src => src.LeaveStockOrder!.UpdatedByName)
                ).ForMember(
                   dto => dto.UpdatedOn,
                   domain => domain.MapFrom(src => src.LeaveStockOrder!.UpdatedOn)
                ).ForMember(
                   dto => dto.CheckedBy,
                   domain => domain.MapFrom(src => src.LeaveStockOrder!.CheckedBy)
                ).ForMember(
                   dto => dto.CheckedByName,
                   domain => domain.MapFrom(src => src.LeaveStockOrder!.CheckedByName)
                ).ForMember(
                   dto => dto.CheckedOn,
                   domain => domain.MapFrom(src => src.LeaveStockOrder!.CheckedOn)
                ).ForMember(
                   dto => dto.Warehouse,
                   domain => domain.MapFrom(src => src.Warehouse)
                ).ForMember(
                   dto => dto.Lens,
                   domain => domain.MapFrom(src => src.Lens)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.LeaveStockOrderDetail_LensDetails.Sum(s => s.Qty))
                );

            #endregion

            #region PurchaseOrder

            CreateMap<PurchaseOrder, PurchaseOrderResponse>()
                .ForMember(
                   dto => dto.PurchaseOrderDetails,
                   domain => domain.MapFrom(src => src.PurchaseOrderDetails)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.PurchaseOrderDetails.SelectMany(s => s.PurchaseOrderDetail_LensDetails).Sum(s => s.Qty))
                ).ForMember(
                   dto => dto.EnterStockQty,
                   domain => domain.MapFrom(src => src.PurchaseOrderDetails.SelectMany(s => s.PurchaseOrderDetail_LensDetails).Sum(s => s.EnterStockQty))
                );

            CreateMap<PurchaseOrderDetail, PurchaseOrderDetailResponse>()
                .ForMember(
                   dto => dto.LensDetails,
                   domain => domain.MapFrom(src => src.PurchaseOrderDetail_LensDetails)
                )
                .ForMember(
                   dto => dto.Supplier,
                   domain => domain.MapFrom(src => src.Supplier)
                ).ForMember(
                   dto => dto.Warehouse,
                   domain => domain.MapFrom(src => src.Warehouse)
                ).ForMember(
                  dto => dto.Lens,
                   domain => domain.MapFrom(src => src.Lens)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.PurchaseOrderDetail_LensDetails.Sum(x => x.Qty))
                ).ForMember(
                   dto => dto.EnterStockQty,
                   domain => domain.MapFrom(src => src.PurchaseOrderDetail_LensDetails.Sum(x => x.EnterStockQty))
                );

            CreateMap<PurchaseOrder, CanEnterStockOrderResponse>()
               .ForMember(
                  dto => dto.EnterStockOrderDetails,
                  domain => domain.MapFrom(src => src.PurchaseOrderDetails)
               ).ForMember(
                  dto => dto.BusinessOrderId,
                  domain => domain.MapFrom(src => src.Id)
               ).ForMember(
                  dto => dto.BusinessOrderNumber,
                  domain => domain.MapFrom(src => src.Number)
               );

            CreateMap<PurchaseOrderDetail, EnterStockOrderDetailResponse>()
                .ForMember(
                   dto => dto.LensDetails,
                   domain => domain.MapFrom(src => src.PurchaseOrderDetail_LensDetails)
                ).ForMember(
                   dto => dto.Warehouse,
                   domain => domain.MapFrom(src => src.Warehouse)
                ).ForMember(
                  dto => dto.Lens,
                   domain => domain.MapFrom(src => src.Lens)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.PurchaseOrderDetail_LensDetails.Sum(x => x.Qty))
                ).ForMember(
                   dto => dto.BusinessOrderDetailId,
                   domain => domain.MapFrom(src => src.Id)
                );

            CreateMap<PurchaseOrderDetail, PurchaseOrderMainInfoResponse>()
                .ForMember(
                   dto => dto.LensDetails,
                   domain => domain.MapFrom(src => src.PurchaseOrderDetail_LensDetails)
                ).ForMember(
                   dto => dto.Number,
                   domain => domain.MapFrom(src => src.PurchaseOrder!.Number)
                ).ForMember(
                   dto => dto.CustomerOrderNumber,
                   domain => domain.MapFrom(src => src.PurchaseOrder!.CustomerOrderNumber)
                ).ForMember(
                   dto => dto.Status,
                   domain => domain.MapFrom(src => src.PurchaseOrder!.Status.GetDescription<PurchaseOrderStatusEnum>())
                ).ForMember(
                   dto => dto.Remark,
                   domain => domain.MapFrom(src => src.PurchaseOrder!.Remark)
                ).ForMember(
                   dto => dto.CreatedBy,
                   domain => domain.MapFrom(src => src.PurchaseOrder!.CreatedBy)
                ).ForMember(
                   dto => dto.CreatedByName,
                   domain => domain.MapFrom(src => src.PurchaseOrder!.CreatedByName)
                ).ForMember(
                   dto => dto.CreatedOn,
                   domain => domain.MapFrom(src => src.PurchaseOrder!.CreatedOn)
                ).ForMember(
                   dto => dto.UpdatedBy,
                   domain => domain.MapFrom(src => src.PurchaseOrder!.UpdatedBy)
                ).ForMember(
                   dto => dto.UpdatedByName,
                   domain => domain.MapFrom(src => src.PurchaseOrder!.UpdatedByName)
                ).ForMember(
                   dto => dto.UpdatedOn,
                   domain => domain.MapFrom(src => src.PurchaseOrder!.UpdatedOn)
                ).ForMember(
                   dto => dto.CheckedBy,
                   domain => domain.MapFrom(src => src.PurchaseOrder!.CheckedBy)
                ).ForMember(
                   dto => dto.CheckedByName,
                   domain => domain.MapFrom(src => src.PurchaseOrder!.CheckedByName)
                ).ForMember(
                   dto => dto.CheckedOn,
                   domain => domain.MapFrom(src => src.PurchaseOrder!.CheckedOn)
                ).ForMember(
                   dto => dto.FinishedBy,
                   domain => domain.MapFrom(src => src.PurchaseOrder!.FinishedBy)
                ).ForMember(
                   dto => dto.FinishedByName,
                   domain => domain.MapFrom(src => src.PurchaseOrder!.FinishedByName)
                ).ForMember(
                   dto => dto.FinishedOn,
                   domain => domain.MapFrom(src => src.PurchaseOrder!.FinishedOn)
                ).ForMember(
                   dto => dto.Supplier,
                   domain => domain.MapFrom(src => src.Supplier)
                ).ForMember(
                   dto => dto.Warehouse,
                   domain => domain.MapFrom(src => src.Warehouse)
                ).ForMember(
                   dto => dto.Lens,
                   domain => domain.MapFrom(src => src.Lens)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.PurchaseOrderDetail_LensDetails.Sum(x => x.Qty))
                ).ForMember(
                   dto => dto.EnterStockQty,
                   domain => domain.MapFrom(src => src.PurchaseOrderDetail_LensDetails.Sum(x => x.EnterStockQty))
                );

            CreateMap<PurchaseOrderDetail_LensDetail, LensDetailResponse>();

            #endregion

            #region TransferOrder

            CreateMap<TransferOrder, TransferOrderResponse>()
                .ForMember(
                   dto => dto.TransferOrderDetails,
                   domain => domain.MapFrom(src => src.TransferOrderDetails)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.TransferOrderDetails.SelectMany(s => s.TransferOrderDetail_LensDetails).Sum(s => s.Qty))
                ).ForMember(
                   dto => dto.LeaveStockQty,
                   domain => domain.MapFrom(src => src.TransferOrderDetails.SelectMany(s => s.TransferOrderDetail_LensDetails).Sum(s => s.LeaveStockQty))
                ).ForMember(
                   dto => dto.EnterStockQty,
                   domain => domain.MapFrom(src => src.TransferOrderDetails.SelectMany(s => s.TransferOrderDetail_LensDetails).Sum(s => s.EnterStockQty))
                );

            CreateMap<TransferOrderDetail, TransferOrderDetailResponse>()
                .ForMember(
                   dto => dto.LensDetails,
                   domain => domain.MapFrom(src => src.TransferOrderDetail_LensDetails)
                ).ForMember(
                   dto => dto.OutWarehouse,
                   domain => domain.MapFrom(src => src.OutWarehouse)
                ).ForMember(
                   dto => dto.InWarehouse,
                   domain => domain.MapFrom(src => src.InWarehouse)
                ).ForMember(
                  dto => dto.Lens,
                   domain => domain.MapFrom(src => src.Lens)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.TransferOrderDetail_LensDetails!.Sum(x => x.Qty))
                ).ForMember(
                   dto => dto.LeaveStockQty,
                   domain => domain.MapFrom(src => src.TransferOrderDetail_LensDetails.Sum(s => s.LeaveStockQty))
                ).ForMember(
                   dto => dto.EnterStockQty,
                   domain => domain.MapFrom(src => src.TransferOrderDetail_LensDetails!.Sum(x => x.EnterStockQty))
                );

            CreateMap<TransferOrder, CanLeaveStockOrderResponse>()
               .ForMember(
                  dto => dto.LeaveStockOrderDetails,
                  domain => domain.MapFrom(src => src.TransferOrderDetails)
               ).ForMember(
                  dto => dto.BusinessOrderId,
                  domain => domain.MapFrom(src => src.Id)
               ).ForMember(
                  dto => dto.BusinessOrderNumber,
                  domain => domain.MapFrom(src => src.Number)
               );

            CreateMap<TransferOrderDetail, LeaveStockOrderDetailResponse>()
                .ForMember(
                   dto => dto.LensDetails,
                   domain => domain.MapFrom(src => src.TransferOrderDetail_LensDetails)
                ).ForMember(
                   dto => dto.WarehouseId,
                   domain => domain.MapFrom(src => src.OutWarehouseId)
                ).ForMember(
                   dto => dto.Warehouse,
                   domain => domain.MapFrom(src => src.OutWarehouse)
                ).ForMember(
                  dto => dto.Lens,
                  domain => domain.MapFrom(src => src.Lens)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.TransferOrderDetail_LensDetails.Sum(x => x.Qty))
                ).ForMember(
                   dto => dto.BusinessOrderDetailId,
                   domain => domain.MapFrom(src => src.Id)
                );

            CreateMap<TransferOrderDetail, TransferOrderMainInfoResponse>()
                .ForMember(
                   dto => dto.LensDetails,
                   domain => domain.MapFrom(src => src.TransferOrderDetail_LensDetails)
                ).ForMember(
                   dto => dto.Number,
                   domain => domain.MapFrom(src => src.TransferOrder!.Number)
                ).ForMember(
                   dto => dto.Status,
                   domain => domain.MapFrom(src => src.TransferOrder!.Status.GetDescription<TransferOrderStatusEnum>())
                ).ForMember(
                   dto => dto.Remark,
                   domain => domain.MapFrom(src => src.TransferOrder!.Remark)
                ).ForMember(
                   dto => dto.CreatedBy,
                   domain => domain.MapFrom(src => src.TransferOrder!.CreatedBy)
                ).ForMember(
                   dto => dto.CreatedByName,
                   domain => domain.MapFrom(src => src.TransferOrder!.CreatedByName)
                ).ForMember(
                   dto => dto.CreatedOn,
                   domain => domain.MapFrom(src => src.TransferOrder!.CreatedOn)
                ).ForMember(
                   dto => dto.UpdatedBy,
                   domain => domain.MapFrom(src => src.TransferOrder!.UpdatedBy)
                ).ForMember(
                   dto => dto.UpdatedByName,
                   domain => domain.MapFrom(src => src.TransferOrder!.UpdatedByName)
                ).ForMember(
                   dto => dto.UpdatedOn,
                   domain => domain.MapFrom(src => src.TransferOrder!.UpdatedOn)
                ).ForMember(
                   dto => dto.CheckedBy,
                   domain => domain.MapFrom(src => src.TransferOrder!.CheckedBy)
                ).ForMember(
                   dto => dto.CheckedByName,
                   domain => domain.MapFrom(src => src.TransferOrder!.CheckedByName)
                ).ForMember(
                   dto => dto.CheckedOn,
                   domain => domain.MapFrom(src => src.TransferOrder!.CheckedOn)
                ).ForMember(
                   dto => dto.FinishedBy,
                   domain => domain.MapFrom(src => src.TransferOrder!.FinishedBy)
                ).ForMember(
                   dto => dto.FinishedByName,
                   domain => domain.MapFrom(src => src.TransferOrder!.FinishedByName)
                ).ForMember(
                   dto => dto.FinishedOn,
                   domain => domain.MapFrom(src => src.TransferOrder!.FinishedOn)
                ).ForMember(
                   dto => dto.OutWarehouse,
                   domain => domain.MapFrom(src => src.OutWarehouse)
                ).ForMember(
                   dto => dto.InWarehouse,
                   domain => domain.MapFrom(src => src.InWarehouse)
                ).ForMember(
                   dto => dto.Lens,
                   domain => domain.MapFrom(src => src.Lens)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.TransferOrderDetail_LensDetails.Sum(x => x.Qty))
                ).ForMember(
                   dto => dto.LeaveStockQty,
                   domain => domain.MapFrom(src => src.TransferOrderDetail_LensDetails.Sum(x => x.LeaveStockQty))
                ).ForMember(
                   dto => dto.EnterStockQty,
                   domain => domain.MapFrom(src => src.TransferOrderDetail_LensDetails.Sum(x => x.EnterStockQty))
                );

            CreateMap<TransferOrderDetail_LensDetail, LensDetailResponse>();

            CreateMap<TransferOrderDetail, LensViewResponse>()
                .ForMember(
                  dto => dto.Warehouse,
                  domain => domain.MapFrom(src => src.InWarehouse)
               ).ForMember(
                 dto => dto.Lens,
                  domain => domain.MapFrom(src => src.Lens)
               ).ForMember(
                 dto => dto.LensDetails,
                  domain => domain.MapFrom(src => src.TransferOrderDetail_LensDetails)
               );

            //CreateMap<TransferOrder, OrderReceiveResponse>().ForMember(
            //       dto => dto.WarehouseId,
            //       domain => domain.MapFrom(src => src.InWarehouse!.Id)
            //    ).ForMember(
            //       dto => dto.WarehouseCode,
            //       domain => domain.MapFrom(src => src.InWarehouse == null ? "" : src.InWarehouse.Code)
            //    ).ForMember(
            //       dto => dto.WarehouseName,
            //       domain => domain.MapFrom(src => src.InWarehouse == null ? "" : src.InWarehouse.Name)
            //    ).ForMember(
            //      dto => dto.LensCode,
            //       domain => domain.MapFrom(src => src.Lens == null ? "" : src.Lens.FactoryLensCode)
            //    ).ForMember(
            //      dto => dto.LensName,
            //       domain => domain.MapFrom(src => src.Lens == null ? "" : src.Lens.FactoryLensName)
            //    );
            #endregion

            #region TransferLensOrder

            CreateMap<TransferLensOrder, TransferLensOrderResponse>()
                .ForMember(
                   dto => dto.TransferLensOrderDetails,
                   domain => domain.MapFrom(src => src.TransferLensOrderDetails)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.TransferLensOrderDetails.SelectMany(s => s.TransferLensOrderDetail_LensDetails).Sum(s => s.Qty))
                ).ForMember(
                   dto => dto.LeaveStockQty,
                   domain => domain.MapFrom(src => src.TransferLensOrderDetails.SelectMany(s => s.TransferLensOrderDetail_LensDetails).Sum(s => s.LeaveStockQty))
                ).ForMember(
                   dto => dto.EnterStockQty,
                   domain => domain.MapFrom(src => src.TransferLensOrderDetails.SelectMany(s => s.TransferLensOrderDetail_LensDetails).Sum(s => s.EnterStockQty))
                );

            CreateMap<TransferLensOrderDetail, TransferLensOrderDetailResponse>()
                .ForMember(
                   dto => dto.LensDetails,
                   domain => domain.MapFrom(src => src.TransferLensOrderDetail_LensDetails)
                ).ForMember(
                   dto => dto.OutWarehouse,
                   domain => domain.MapFrom(src => src.OutWarehouse)
                ).ForMember(
                   dto => dto.InWarehouse,
                   domain => domain.MapFrom(src => src.InWarehouse)
                ).ForMember(
                  dto => dto.OutLens,
                   domain => domain.MapFrom(src => src.OutLens)
                ).ForMember(
                  dto => dto.InLens,
                   domain => domain.MapFrom(src => src.InLens)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.TransferLensOrderDetail_LensDetails.Sum(x => x.Qty))
                ).ForMember(
                   dto => dto.LeaveStockQty,
                   domain => domain.MapFrom(src => src.TransferLensOrderDetail_LensDetails.Sum(s => s.LeaveStockQty))
                ).ForMember(
                   dto => dto.EnterStockQty,
                   domain => domain.MapFrom(src => src.TransferLensOrderDetail_LensDetails.Sum(s => s.EnterStockQty))
                );

            CreateMap<TransferLensOrder, CanLeaveStockOrderResponse>()
               .ForMember(
                  dto => dto.LeaveStockOrderDetails,
                  domain => domain.MapFrom(src => src.TransferLensOrderDetails)
               ).ForMember(
                  dto => dto.BusinessOrderId,
                  domain => domain.MapFrom(src => src.Id)
               ).ForMember(
                  dto => dto.BusinessOrderNumber,
                  domain => domain.MapFrom(src => src.Number)
               );

            CreateMap<TransferLensOrderDetail, LeaveStockOrderDetailResponse>()
                .ForMember(
                   dto => dto.LensDetails,
                   domain => domain.MapFrom(src => src.TransferLensOrderDetail_LensDetails)
                ).ForMember(
                   dto => dto.WarehouseId,
                   domain => domain.MapFrom(src => src.OutWarehouseId)
                ).ForMember(
                   dto => dto.Warehouse,
                   domain => domain.MapFrom(src => src.OutWarehouse)
                ).ForMember(
                  dto => dto.LensId,
                  domain => domain.MapFrom(src => src.OutLensId)
                ).ForMember(
                  dto => dto.Lens,
                  domain => domain.MapFrom(src => src.OutLens)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.TransferLensOrderDetail_LensDetails.Sum(x => x.Qty))
                ).ForMember(
                   dto => dto.BusinessOrderDetailId,
                   domain => domain.MapFrom(src => src.Id)
                );

            CreateMap<TransferLensOrderDetail, TransferLensOrderMainInfoResponse>()
                .ForMember(
                   dto => dto.LensDetails,
                   domain => domain.MapFrom(src => src.TransferLensOrderDetail_LensDetails)
                ).ForMember(
                   dto => dto.Number,
                   domain => domain.MapFrom(src => src.TransferLensOrder!.Number)
                ).ForMember(
                   dto => dto.Status,
                   domain => domain.MapFrom(src => src.TransferLensOrder!.Status.GetDescription<TransferLensOrderStatusEnum>())
                ).ForMember(
                   dto => dto.Remark,
                   domain => domain.MapFrom(src => src.TransferLensOrder!.Remark)
                ).ForMember(
                   dto => dto.CreatedBy,
                   domain => domain.MapFrom(src => src.TransferLensOrder!.CreatedBy)
                ).ForMember(
                   dto => dto.CreatedByName,
                   domain => domain.MapFrom(src => src.TransferLensOrder!.CreatedByName)
                ).ForMember(
                   dto => dto.CreatedOn,
                   domain => domain.MapFrom(src => src.TransferLensOrder!.CreatedOn)
                ).ForMember(
                   dto => dto.UpdatedBy,
                   domain => domain.MapFrom(src => src.TransferLensOrder!.UpdatedBy)
                ).ForMember(
                   dto => dto.UpdatedByName,
                   domain => domain.MapFrom(src => src.TransferLensOrder!.UpdatedByName)
                ).ForMember(
                   dto => dto.UpdatedOn,
                   domain => domain.MapFrom(src => src.TransferLensOrder!.UpdatedOn)
                ).ForMember(
                   dto => dto.CheckedBy,
                   domain => domain.MapFrom(src => src.TransferLensOrder!.CheckedBy)
                ).ForMember(
                   dto => dto.CheckedByName,
                   domain => domain.MapFrom(src => src.TransferLensOrder!.CheckedByName)
                ).ForMember(
                   dto => dto.CheckedOn,
                   domain => domain.MapFrom(src => src.TransferLensOrder!.CheckedOn)
                ).ForMember(
                   dto => dto.FinishedBy,
                   domain => domain.MapFrom(src => src.TransferLensOrder!.FinishedBy)
                ).ForMember(
                   dto => dto.FinishedByName,
                   domain => domain.MapFrom(src => src.TransferLensOrder!.FinishedByName)
                ).ForMember(
                   dto => dto.FinishedOn,
                   domain => domain.MapFrom(src => src.TransferLensOrder!.FinishedOn)
                ).ForMember(
                   dto => dto.OutWarehouse,
                   domain => domain.MapFrom(src => src.OutWarehouse)
                ).ForMember(
                   dto => dto.OutLens,
                   domain => domain.MapFrom(src => src.OutLens)
                ).ForMember(
                   dto => dto.InWarehouse,
                   domain => domain.MapFrom(src => src.InWarehouse)
                ).ForMember(
                   dto => dto.InLens,
                   domain => domain.MapFrom(src => src.InLens)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.TransferLensOrderDetail_LensDetails.Sum(x => x.Qty))
                ).ForMember(
                   dto => dto.LeaveStockQty,
                   domain => domain.MapFrom(src => src.TransferLensOrderDetail_LensDetails.Sum(x => x.LeaveStockQty))
                ).ForMember(
                   dto => dto.EnterStockQty,
                   domain => domain.MapFrom(src => src.TransferLensOrderDetail_LensDetails.Sum(x => x.EnterStockQty))
                );

            CreateMap<TransferLensOrderDetail_LensDetail, LensDetailResponse>();

            #endregion

            #region InventoryOrder

            CreateMap<InventoryOrder, InventoryOrderResponse>()
                .ForMember(
                   dto => dto.InventoryOrderDetails,
                   domain => domain.MapFrom(src => src.InventoryOrderDetails)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.InventoryOrderDetails.SelectMany(s => s.InventoryOrderDetail_LensDetails).Sum(s => s.Qty))
                ).ForMember(
                   dto => dto.ProfitQty,
                   domain => domain.MapFrom(src => src.InventoryOrderDetails.SelectMany(s => s.InventoryOrderDetail_LensDetails).Sum(s => s.ProfitQty))
                ).ForMember(
                   dto => dto.LossQty,
                   domain => domain.MapFrom(src => src.InventoryOrderDetails.SelectMany(s => s.InventoryOrderDetail_LensDetails).Sum(s => s.LossQty))
                );

            CreateMap<InventoryOrderDetail, InventoryOrderDetailResponse>()
                .ForMember(
                   dto => dto.LensDetails,
                   domain => domain.MapFrom(src => src.InventoryOrderDetail_LensDetails)
                ).ForMember(
                   dto => dto.Warehouse,
                   domain => domain.MapFrom(src => src.Warehouse)
                ).ForMember(
                  dto => dto.Lens,
                   domain => domain.MapFrom(src => src.Lens)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.InventoryOrderDetail_LensDetails!.Sum(x => x.Qty))
                )
                .ForMember(
                   dto => dto.ProfitQty,
                   domain => domain.MapFrom(src => src.InventoryOrderDetail_LensDetails!.Sum(x => x.ProfitQty))
                )
                .ForMember(
                   dto => dto.LossQty,
                   domain => domain.MapFrom(src => src.InventoryOrderDetail_LensDetails!.Sum(x => x.LossQty))
                );

            CreateMap<InventoryOrderDetail, InventoryOrderMainInfoResponse>()
                .ForMember(
                   dto => dto.LensDetails,
                   domain => domain.MapFrom(src => src.InventoryOrderDetail_LensDetails)
                ).ForMember(
                   dto => dto.Number,
                   domain => domain.MapFrom(src => src.InventoryOrder!.Number)
                ).ForMember(
                   dto => dto.Status,
                   domain => domain.MapFrom(src => src.InventoryOrder!.Status.GetDescription<InventoryOrderStatusEnum>())
                ).ForMember(
                   dto => dto.Remark,
                   domain => domain.MapFrom(src => src.InventoryOrder!.Remark)
                ).ForMember(
                   dto => dto.CreatedBy,
                   domain => domain.MapFrom(src => src.InventoryOrder!.CreatedBy)
                ).ForMember(
                   dto => dto.CreatedByName,
                   domain => domain.MapFrom(src => src.InventoryOrder!.CreatedByName)
                ).ForMember(
                   dto => dto.CreatedOn,
                   domain => domain.MapFrom(src => src.InventoryOrder!.CreatedOn)
                ).ForMember(
                   dto => dto.UpdatedBy,
                   domain => domain.MapFrom(src => src.InventoryOrder!.UpdatedBy)
                ).ForMember(
                   dto => dto.UpdatedByName,
                   domain => domain.MapFrom(src => src.InventoryOrder!.UpdatedByName)
                ).ForMember(
                   dto => dto.UpdatedOn,
                   domain => domain.MapFrom(src => src.InventoryOrder!.UpdatedOn)
                ).ForMember(
                   dto => dto.CheckedBy,
                   domain => domain.MapFrom(src => src.InventoryOrder!.CheckedBy)
                ).ForMember(
                   dto => dto.CheckedByName,
                   domain => domain.MapFrom(src => src.InventoryOrder!.CheckedByName)
                ).ForMember(
                   dto => dto.CheckedOn,
                   domain => domain.MapFrom(src => src.InventoryOrder!.CheckedOn)
                ).ForMember(
                   dto => dto.FinishedBy,
                   domain => domain.MapFrom(src => src.InventoryOrder!.FinishedBy)
                ).ForMember(
                   dto => dto.FinishedByName,
                   domain => domain.MapFrom(src => src.InventoryOrder!.FinishedByName)
                ).ForMember(
                   dto => dto.FinishedOn,
                   domain => domain.MapFrom(src => src.InventoryOrder!.FinishedOn)
                ).ForMember(
                   dto => dto.Warehouse,
                   domain => domain.MapFrom(src => src.Warehouse)
                ).ForMember(
                   dto => dto.Lens,
                   domain => domain.MapFrom(src => src.Lens)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.InventoryOrderDetail_LensDetails.Sum(x => x.Qty))
                ).ForMember(
                   dto => dto.ProfitQty,
                   domain => domain.MapFrom(src => src.InventoryOrderDetail_LensDetails.Sum(x => x.ProfitQty))
                ).ForMember(
                   dto => dto.LossQty,
                   domain => domain.MapFrom(src => src.InventoryOrderDetail_LensDetails.Sum(x => x.LossQty))
                );

            CreateMap<InventoryOrderDetail_LensDetail, LensDetailResponse>();

            #endregion

            #region StockLogDetail
            //CreateMap<StockLogDetail, LensDetail>().ForMember(
            //       dto => dto.WarehouseId,
            //       domain => domain.MapFrom(src => src.StockLog!.WarehouseId)
            //    ).ForMember(
            //      dto => dto.LensId,
            //       domain => domain.MapFrom(src => src.StockLog!.LensId)
            //    ).ForMember(
            //       dto => dto.WarehouseCode,
            //       domain => domain.MapFrom(src => src.StockLog!.Warehouse == null ? "" : src.StockLog.Warehouse.Code)
            //    ).ForMember(
            //      dto => dto.LensCode,
            //       domain => domain.MapFrom(src => src.StockLog!.Lens == null ? "" : src.StockLog.Lens.FactoryLensCode)
            //    );
            #endregion

            #region Customer
            
            CreateMap<Customer, CustomerResponse>();

            #endregion

            #region Contract

            CreateMap<Contract, ContractResponse>()
                .ForMember(
                   dto => dto.CustomerIds,
                   domain => domain.MapFrom(src => src.Contract_Customers == null ? null : src.Contract_Customers.Select(x => x.CustomerId))
                )
                .ForMember(
                   dto => dto.CustomerNames,
                   domain => domain.MapFrom(src => src.Contract_Customers == null ? null : src.Contract_Customers.Select(x => x.Customer == null ? "" : x.Customer.Name))
                );
                //.ForMember(
                //   dto => dto.Contract_Lenses,
                //   domain => domain.MapFrom(src => src.Contract_Lenses)

                //).ForMember(
                //   dto => dto.Contract_Lens_SalesProcessConstraints,
                //   domain => domain.MapFrom(src => src.Contract_Lens_SalesProcessConstraints)
                //);

            CreateMap<Contract_Lens, Contract_LensResponse>()
                .ForMember(
                   dto => dto.LensCode,
                   domain => domain.MapFrom(src => src.Lens == null ? "" : src.Lens.SalesLensCode)
                ).ForMember(
                   dto => dto.Lens,
                   domain => domain.MapFrom(src => src.Lens)
                );

            CreateMap<Contract_Lens_SalesProcessConstraint, Contract_Lens_SalesProcessConstraintResponse>()
                .ForMember(
                   dto => dto.LensCode,
                   domain => domain.MapFrom(src => src.Lens == null ? "" : src.Lens.SalesLensCode)
                );
            #endregion

            #region RetailOrder

            //CreateMap<RetailOrder, RetailOrderResponse>()
            //    .ForMember(
            //       dto => dto.RetailOrderDetails,
            //       domain => domain.MapFrom(src => src.RetailOrderDetails)
            //    ).ForMember(
            //       dto => dto.Contract,
            //       domain => domain.MapFrom(src => src.Contract)
            //    ).ForMember(
            //       dto => dto.Customer,
            //       domain => domain.MapFrom(src => src.Customer)
            //    ).ForMember(
            //       dto => dto.Qty,
            //       domain => domain.MapFrom(src => src.RetailOrderDetails.Sum(s => s.Qty))
            //    ).ForMember(
            //        dto => dto.RetailOrderSalesProcess,
            //        domain => domain.MapFrom((src, dto) =>
            //        {
            //            RetailOrderSalesProcessResponse destination = new()
            //            {
            //                Coating = src.RetailOrder_SalesProcesses.Select(s => s.SalesProcess!).FirstOrDefault(x => x.Code == SalesProcessesTypeEnum.Coating.ToString())?.Name,
            //                TintingColor = src.RetailOrder_SalesProcesses.Select(s => s.SalesProcess!).FirstOrDefault(x => x.Code == SalesProcessesTypeEnum.TintingColor.ToString())?.Name,
            //                Mirror = src.RetailOrder_SalesProcesses.Select(s => s.SalesProcess!).FirstOrDefault(x => x.Code == SalesProcessesTypeEnum.Mirror.ToString())?.Name,
            //                TintingCode = src.RetailOrder_SalesProcesses.Select(s => s.SalesProcess!).FirstOrDefault(x => x.Code == SalesProcessesTypeEnum.TintingCode.ToString())?.Name,
            //                TintingName = src.RetailOrder_SalesProcesses.Select(s => s.SalesProcess!).FirstOrDefault(x => x.Code == SalesProcessesTypeEnum.TintingName.ToString())?.Name,
            //                UV = src.RetailOrder_SalesProcesses.Select(s => s.SalesProcess!).FirstOrDefault(x => x.Code == SalesProcessesTypeEnum.UV.ToString()) == null ? false : true,
            //                Frame = src.RetailOrder_SalesProcesses.Select(s => s.SalesProcess!).FirstOrDefault(x => x.Code == SalesProcessesTypeEnum.Frame.ToString())?.Name,
            //                Hard = src.RetailOrder_SalesProcesses.Select(s => s.SalesProcess!).FirstOrDefault(x => x.Code == SalesProcessesTypeEnum.Hard.ToString()) == null ? false : true,
            //                PaoGuang = src.RetailOrder_SalesProcesses.Select(s => s.SalesProcess!).FirstOrDefault(x => x.Code == SalesProcessesTypeEnum.PaoGuang.ToString()) == null ? false : true,
            //                CaiBian = src.RetailOrder_SalesProcesses.Select(s => s.SalesProcess!).FirstOrDefault(x => x.Code == SalesProcessesTypeEnum.CaiBian.ToString())?.Name,
            //                CheBian = src.RetailOrder_SalesProcesses.Select(s => s.SalesProcess!).FirstOrDefault(x => x.Code == SalesProcessesTypeEnum.CheBian.ToString())?.Name,
            //                KaiKeng = src.RetailOrder_SalesProcesses.Select(s => s.SalesProcess!).FirstOrDefault(x => x.Code == SalesProcessesTypeEnum.KaiKeng.ToString())?.Name,
            //                PiHua = src.RetailOrder_SalesProcesses.Select(s => s.SalesProcess!).FirstOrDefault(x => x.Code == SalesProcessesTypeEnum.PiHua.ToString())?.Name,
            //                KnifeEdge = src.RetailOrder_SalesProcesses.Select(s => s.SalesProcess!).FirstOrDefault(x => x.Code == SalesProcessesTypeEnum.KnifeEdge.ToString())?.Name,
            //                Base = src.RetailOrder_SalesProcesses.Select(s => s.SalesProcess!).FirstOrDefault(x => x.Code == SalesProcessesTypeEnum.Base.ToString())?.Name,
            //                ZuanKong = src.RetailOrder_SalesProcesses.Select(s => s.SalesProcess!).FirstOrDefault(x => x.Code == SalesProcessesTypeEnum.ZuanKong.ToString())?.Name,
            //                OtherProcess = src.RetailOrder_SalesProcesses.Select(s => s.SalesProcess!).FirstOrDefault(x => x.Code == SalesProcessesTypeEnum.OtherProcess.ToString())?.Name,
            //                ExtraProcess = src.RetailOrder_SalesProcesses.Select(s => s.SalesProcess!).FirstOrDefault(x => x.Code == SalesProcessesTypeEnum.ExtraProcess.ToString())?.Name
            //            };
            //            return destination;
            //        })
            //    );

            CreateMap<RetailOrder, RetailOrderResponse>()
                .ForMember(
                   dto => dto.RetailOrderDetails,
                   domain => domain.MapFrom(src => src.RetailOrderDetails)
                ).ForMember(
                   dto => dto.Contract,
                   domain => domain.MapFrom(src => src.Contract)
                ).ForMember(
                   dto => dto.Customer,
                   domain => domain.MapFrom(src => src.Customer)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.RetailOrderDetails.Sum(s => s.Qty))
                );

            CreateMap<RetailOrderDetail, RetailOrderDetailResponse>()
                .ForMember(
                   dto => dto.Warehouse,
                   domain => domain.MapFrom(src => src.Warehouse)
                )
                .ForMember(
                   dto => dto.Contract_Lens,
                   domain => domain.MapFrom(src => src.Contract_Lens)
                );

            CreateMap<RetailOrderDetail, RetailOrderMainInfoResponse>()
                .ForMember(
                   dto => dto.Number,
                   domain => domain.MapFrom(src => src.RetailOrder!.Number)
                ).ForMember(
                   dto => dto.RelationNumber,
                   domain => domain.MapFrom(src => src.RetailOrder!.RelationNumber)
                ).ForMember(
                   dto => dto.CustomerOrderNumber,
                   domain => domain.MapFrom(src => src.RetailOrder!.CustomerOrderNumber)
                ).ForMember(
                   dto => dto.TrayNumber,
                   domain => domain.MapFrom(src => src.RetailOrder!.TrayNumber)
                ).ForMember(
                   dto => dto.DeliveryOn,
                   domain => domain.MapFrom(src => src.RetailOrder!.DeliveryOn)
                ).ForMember(
                   dto => dto.OutRank,
                   domain => domain.MapFrom(src => src.RetailOrder!.OutRank)
                ).ForMember(
                   dto => dto.Material,
                   domain => domain.MapFrom(src => src.RetailOrder!.Material)
                ).ForMember(
                   dto => dto.Status,
                   domain => domain.MapFrom(src => src.RetailOrder!.Status.GetDescription<RetailOrderStatusEnum>())
                ).ForMember(
                   dto => dto.InvoicePrintOn,
                   domain => domain.MapFrom(src => src.RetailOrder!.InvoicePrintOn)
                ).ForMember(
                   dto => dto.Remark,
                   domain => domain.MapFrom(src => src.RetailOrder!.Remark)
                ).ForMember(
                   dto => dto.CreatedBy,
                   domain => domain.MapFrom(src => src.RetailOrder!.CreatedBy)
                ).ForMember(
                   dto => dto.CreatedByName,
                   domain => domain.MapFrom(src => src.RetailOrder!.CreatedByName)
                ).ForMember(
                   dto => dto.CreatedOn,
                   domain => domain.MapFrom(src => src.RetailOrder!.CreatedOn)
                ).ForMember(
                   dto => dto.UpdatedBy,
                   domain => domain.MapFrom(src => src.RetailOrder!.UpdatedBy)
                ).ForMember(
                   dto => dto.UpdatedByName,
                   domain => domain.MapFrom(src => src.RetailOrder!.UpdatedByName)
                ).ForMember(
                   dto => dto.UpdatedOn,
                   domain => domain.MapFrom(src => src.RetailOrder!.UpdatedOn)
                ).ForMember(
                   dto => dto.CheckedBy,
                   domain => domain.MapFrom(src => src.RetailOrder!.CheckedBy)
                ).ForMember(
                   dto => dto.CheckedByName,
                   domain => domain.MapFrom(src => src.RetailOrder!.CheckedByName)
                ).ForMember(
                   dto => dto.CheckedOn,
                   domain => domain.MapFrom(src => src.RetailOrder!.CheckedOn)
                ).ForMember(
                   dto => dto.FinishedBy,
                   domain => domain.MapFrom(src => src.RetailOrder!.FinishedBy)
                ).ForMember(
                   dto => dto.FinishedByName,
                   domain => domain.MapFrom(src => src.RetailOrder!.FinishedByName)
                ).ForMember(
                   dto => dto.FinishedOn,
                   domain => domain.MapFrom(src => src.RetailOrder!.FinishedOn)
                ).ForMember(
                   dto => dto.Contract,
                   domain => domain.MapFrom(src => src.RetailOrder!.Contract)
                ).ForMember(
                   dto => dto.Customer,
                   domain => domain.MapFrom(src => src.RetailOrder!.Customer)
                ).ForMember(
                   dto => dto.Warehouse,
                   domain => domain.MapFrom(src => src.Warehouse)
                ).ForMember(
                   dto => dto.Contract_Lens,
                   domain => domain.MapFrom(src => src.Contract_Lens)
                );

            CreateMap<RetailOrderDetail, LensDetailResponse>();

            CreateMap<ASFormat, RetailOrderSaveRequest>()
                .ForMember(
                   dto => dto.CustomerOrderNumber,
                   domain => domain.MapFrom(src => src.ORDER_NO)
                )
                .ForMember(
                   dto => dto.OutRank,
                   domain => domain.MapFrom(src => src.OUT_GOOD)
                )
                .ForMember(
                   dto => dto.DeliveryOn,
                   domain => domain.MapFrom(src => src.OUT_GOODTIME)
                )
                .ForMember(
                   dto => dto.Material,
                   domain => domain.MapFrom(src => src.LENS_MATERIAL)
                )
                .ForMember(
                   dto => dto.Remark,
                   domain => domain.MapFrom(src => src.REMARK)
                ).ForMember(
                   dto => dto.Coating,
                   domain => domain.MapFrom(src => src.COATING)
                )
                .ForMember(
                   dto => dto.Mirror,
                   domain => domain.MapFrom(src => src.MIR)
                )
                .ForMember(
                   dto => dto.TintingCode,
                   domain => domain.MapFrom(src => src.TINT_CODE)
                )
                .ForMember(
                   dto => dto.TintingName,
                   domain => domain.MapFrom(src => src.TINT_CONTENT)
                )
                .ForMember(
                   dto => dto.UV,
                   domain => domain.MapFrom(src => src.UV)
                )
                .ForMember(
                   dto => dto.Hard,
                   domain => domain.MapFrom(src => src.HARD)
                ).ForMember(
                    dto => dto.RetailOrderDetails,
                    domain => domain.MapFrom((src, dto) =>
                    {
                        List<RetailOrderDetailSaveRequest> destination = new();
                        //右
                        RetailOrderDetailSaveRequest detailR = new()
                        {
                            EyeType = EyeTypeEnum.R.ToString(),
                            SPH = src.RSPH,
                            CYL = src.RCYL,
                            ADD = src.RADDS,
                            Axis = src.RAXIS,
                            Diameter = src.RLENS_SIZE,
                            KnifeSide = src.RKNIFE_SIDER,
                            Qty = src.RQTY,
                            Base = src.RBASE,
                            SalesLensCode = src.RLENS_TYPE,
                        };
                        destination.Add(detailR);
                        //左
                        RetailOrderDetailSaveRequest detailL = new()
                        {
                            EyeType = EyeTypeEnum.L.ToString(),
                            SPH = src.LSPH,
                            CYL = src.LCYL,
                            ADD = src.LADDS,
                            Axis = src.LAXIS,
                            Diameter = src.LLENS_SIZE,
                            KnifeSide = src.LKNIFE_SIDER,
                            Qty = src.LQTY,
                            Base = src.LBASE,
                            SalesLensCode = src.LLENS_TYPE,
                        };
                        destination.Add(detailL);
                        return destination;
                    })
                );

            CreateMap<MDGlassFormat, RetailOrderSaveRequest>()
                .ForMember(
                   dto => dto.CustomerOrderNumber,
                   domain => domain.MapFrom(src => src.客戶單號)
                ).ForMember(
                   dto => dto.CustomerCode,
                   domain => domain.MapFrom(src => src.客戶代號)
                ).ForMember(
                   dto => dto.OutRank,
                   domain => domain.MapFrom(src => src.擬出貨)
                ).ForMember(
                   dto => dto.DeliveryOn,
                   domain => domain.MapFrom(src => src.擬出時段)
                ).ForMember(
                   dto => dto.Material,
                   domain => domain.MapFrom(src => src.玻璃膠片)
                ).ForMember(
                   dto => dto.Remark,
                   domain => domain.MapFrom(src => src.特別注明)
                ).ForMember(
                   dto => dto.Coating,
                   domain => domain.MapFrom(src => src.焗色)
                ).ForMember(
                   dto => dto.TintingColor,
                   domain => domain.MapFrom(src => src.茶色)
                ).ForMember(
                   dto => dto.CaiBian,
                   domain => domain.MapFrom(src => src.彩邊)
                ).ForMember(
                   dto => dto.Mirror,
                   domain => domain.MapFrom(src => src.水銀)
                ).ForMember(
                   dto => dto.TintingCode,
                   domain => domain.MapFrom(src => src.染色代號)
                ).ForMember(
                   dto => dto.TintingName,
                   domain => domain.MapFrom(src => src.染色名稱)
                ).ForMember(
                   dto => dto.UV,
                   domain => domain.MapFrom(src => src.UV)
                )
                //.ForMember(
                //   dto => dto.Frame,
                //   domain => domain.MapFrom(src => src.JingJia)
                //)
                .ForMember(
                   dto => dto.Hard,
                   domain => domain.MapFrom(src => src.加硬)
                ).ForMember(
                   dto => dto.PaoGuang,
                   domain => domain.MapFrom(src => src.拋光)
                ).ForMember(
                   dto => dto.CheBian,
                   domain => domain.MapFrom(src => src.車邊代號)
                ).ForMember(
                   dto => dto.KaiKeng,
                   domain => domain.MapFrom(src => src.開坑代號)
                ).ForMember(
                   dto => dto.PiHua,
                   domain => domain.MapFrom(src => src.批花代號)
                ).ForMember(
                    dto => dto.RetailOrderDetails,
                    domain => domain.MapFrom((src, dto) =>
                    {
                        List<RetailOrderDetailSaveRequest> destination = new();
                        var pd = Common.GetPDs(src.PD);
                        //右
                        RetailOrderDetailSaveRequest detailR = new()
                        {
                            EyeType = EyeTypeEnum.R.ToString(),
                            SPH = src.SPHR,
                            CYL = src.CYLR,
                            ADD = src.ADDR,
                            Axis = src.AXISR,
                            PD = pd[0],
                            CenterThickness = src.中心R,
                            KnifeSide = src.刀邊R,
                            Base = src.面灣R,
                            Qty = src.片數R,
                            SalesLensCode = src.鏡種類R,
                        };
                        destination.Add(detailR);
                        //左
                        RetailOrderDetailSaveRequest detailL = new()
                        {
                            EyeType = EyeTypeEnum.L.ToString(),
                            SPH = src.SPHL,
                            CYL = src.CYLL,
                            ADD = src.ADDL,
                            Axis = src.AXISL,
                            PD = pd[1],
                            CenterThickness = src.中心L,
                            KnifeSide = src.刀邊L,
                            Base = src.面灣L,
                            Qty = src.片數L,
                            SalesLensCode = src.鏡種類L,
                        };
                        destination.Add(detailL);
                        return destination;
                    })
                );

            #endregion

            #region BulkOrder

            CreateMap<BulkOrder, BulkOrderResponse>()
                .ForMember(
                   dto => dto.BulkOrderDetails,
                   domain => domain.MapFrom(src => src.BulkOrderDetails)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom((src, dto) =>
                   {
                       if (src.Type == BulkOrderTypeEnum.Product.ToString())
                           return src.BulkOrderDetails.SelectMany(s => s.BulkOrderDetail_LensDetails).Sum(x => x.Qty);
                       else if (src.Type == BulkOrderTypeEnum.MonofocalMaterial.ToString())
                           return src.BulkOrderDetails.SelectMany(s => s.BulkOrderDetail_MonofocalMaterialDetails).Sum(x => x.Qty);
                       else if (src.Type == BulkOrderTypeEnum.BifocalMaterial.ToString())
                           return src.BulkOrderDetails.SelectMany(s => s.BulkOrderDetail_BifocalMaterialDetails).Sum(x => x.Qty);
                       else
                           return 0;
                   })
                ).ForMember(
                   dto => dto.LeaveStockQty,
                   domain => domain.MapFrom(src => src.BulkOrderDetails.SelectMany(s => s.BulkOrderDetail_LensDetails).Sum(s => s.LeaveStockQty))
                );

            CreateMap<BulkOrderDetail, BulkOrderDetailResponse>()
                .ForMember(
                   dto => dto.LensDetails,
                   domain => domain.MapFrom(src => src.BulkOrderDetail_LensDetails)
                ).ForMember(
                   dto => dto.BulkOrderDetail_MonofocalMaterialDetails,
                   domain => domain.MapFrom(src => src.BulkOrderDetail_MonofocalMaterialDetails)
                ).ForMember(
                   dto => dto.BulkOrderDetail_BifocalMaterialDetails,
                   domain => domain.MapFrom(src => src.BulkOrderDetail_BifocalMaterialDetails)
                ).ForMember(
                   dto => dto.Supplier,
                   domain => domain.MapFrom(src => src.Supplier)
                ).ForMember(
                   dto => dto.Warehouse,
                   domain => domain.MapFrom(src => src.Warehouse)
                ).ForMember(
                  dto => dto.Contract_Lens,
                   domain => domain.MapFrom(src => src.Contract_Lens)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom((src, dto) =>
                   {
                       if (src.BulkOrder != null)
                       {
                           if (src.BulkOrder.Type == BulkOrderTypeEnum.Product.ToString())
                               return src.BulkOrderDetail_LensDetails.Sum(x => x.Qty);
                           else if (src.BulkOrder.Type == BulkOrderTypeEnum.MonofocalMaterial.ToString())
                               return src.BulkOrderDetail_MonofocalMaterialDetails.Sum(x => x.Qty);
                           else if (src.BulkOrder.Type == BulkOrderTypeEnum.BifocalMaterial.ToString())
                               return src.BulkOrderDetail_BifocalMaterialDetails.Sum(x => x.Qty);
                       }
                       return 0;

                   })
                ).ForMember(
                   dto => dto.LeaveStockQty,
                   domain => domain.MapFrom(src => src.BulkOrderDetail_LensDetails.Sum(x => x.LeaveStockQty))
                );

            CreateMap<BulkOrder, CanLeaveStockOrderResponse>()
               .ForMember(
                  dto => dto.LeaveStockOrderDetails,
                  domain => domain.MapFrom(src => src.BulkOrderDetails)
               ).ForMember(
                  dto => dto.BusinessOrderId,
                  domain => domain.MapFrom(src => src.Id)
               ).ForMember(
                  dto => dto.BusinessOrderNumber,
                  domain => domain.MapFrom(src => src.Number)
               );

            CreateMap<BulkOrderDetail, LeaveStockOrderDetailResponse>()
                .ForMember(
                   dto => dto.LensDetails,
                   domain => domain.MapFrom(src => src.BulkOrderDetail_LensDetails)
                ).ForMember(
                   dto => dto.Warehouse,
                   domain => domain.MapFrom(src => src.Warehouse)
                ).ForMember(
                  dto => dto.Lens,
                   domain => domain.MapFrom(src => src.Contract_Lens!.Lens)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom(src => src.BulkOrderDetail_LensDetails.Sum(x => x.Qty))
                ).ForMember(
                   dto => dto.BusinessOrderDetailId,
                   domain => domain.MapFrom(src => src.Id)
                ).ForMember(
                  dto => dto.LensId,
                   domain => domain.MapFrom(src => src.Contract_Lens!.Lens == null ? 0 : src.Contract_Lens!.Lens.Id)
                );

            CreateMap<BulkOrderDetail, LensViewResponse>()
                .ForMember(
                  dto => dto.Warehouse,
                  domain => domain.MapFrom(src => src.Warehouse)
               ).ForMember(
                 dto => dto.Lens,
                  domain => domain.MapFrom(src => src.Contract_Lens!.Lens)
               ).ForMember(
                 dto => dto.LensDetails,
                  domain => domain.MapFrom(src => src.BulkOrderDetail_LensDetails)
               );

            CreateMap<BulkOrderDetail, BulkOrderMainInfoResponse>()
                .ForMember(
                   dto => dto.LensDetails,
                   domain => domain.MapFrom(src => src.BulkOrderDetail_LensDetails)
                ).ForMember(
                   dto => dto.BulkOrderDetail_MonofocalMaterialDetails,
                   domain => domain.MapFrom(src => src.BulkOrderDetail_MonofocalMaterialDetails)
                ).ForMember(
                   dto => dto.BulkOrderDetail_BifocalMaterialDetails,
                   domain => domain.MapFrom(src => src.BulkOrderDetail_BifocalMaterialDetails)
                ).ForMember(
                   dto => dto.Type,
                   domain => domain.MapFrom(src => src.BulkOrder!.Type.GetDescription<BulkOrderTypeEnum>())
                ).ForMember(
                   dto => dto.Number,
                   domain => domain.MapFrom(src => src.BulkOrder!.Number)
                ).ForMember(
                   dto => dto.CustomerOrderNumber,
                   domain => domain.MapFrom(src => src.BulkOrder!.CustomerOrderNumber)
                ).ForMember(
                   dto => dto.OrderRef,
                   domain => domain.MapFrom(src => src.BulkOrder!.OrderRef)
                ).ForMember(
                   dto => dto.TrayNumber,
                   domain => domain.MapFrom(src => src.BulkOrder!.TrayNumber)
                ).ForMember(
                   dto => dto.DeliveryOn,
                   domain => domain.MapFrom(src => src.BulkOrder!.DeliveryOn)
                ).ForMember(
                   dto => dto.OutRank,
                   domain => domain.MapFrom(src => src.BulkOrder!.OutRank)
                ).ForMember(
                   dto => dto.Status,
                   domain => domain.MapFrom(src => src.BulkOrder!.Status.GetDescription<BulkOrderStatusEnum>())
                ).ForMember(
                   dto => dto.InvoicePrintOn,
                   domain => domain.MapFrom(src => src.BulkOrder!.InvoicePrintOn)
                ).ForMember(
                   dto => dto.Remark,
                   domain => domain.MapFrom(src => src.BulkOrder!.Remark)
                ).ForMember(
                   dto => dto.CreatedBy,
                   domain => domain.MapFrom(src => src.BulkOrder!.CreatedBy)
                ).ForMember(
                   dto => dto.CreatedByName,
                   domain => domain.MapFrom(src => src.BulkOrder!.CreatedByName)
                ).ForMember(
                   dto => dto.CreatedOn,
                   domain => domain.MapFrom(src => src.BulkOrder!.CreatedOn)
                ).ForMember(
                   dto => dto.UpdatedBy,
                   domain => domain.MapFrom(src => src.BulkOrder!.UpdatedBy)
                ).ForMember(
                   dto => dto.UpdatedByName,
                   domain => domain.MapFrom(src => src.BulkOrder!.UpdatedByName)
                ).ForMember(
                   dto => dto.UpdatedOn,
                   domain => domain.MapFrom(src => src.BulkOrder!.UpdatedOn)
                ).ForMember(
                   dto => dto.CheckedBy,
                   domain => domain.MapFrom(src => src.BulkOrder!.CheckedBy)
                ).ForMember(
                   dto => dto.CheckedByName,
                   domain => domain.MapFrom(src => src.BulkOrder!.CheckedByName)
                ).ForMember(
                   dto => dto.CheckedOn,
                   domain => domain.MapFrom(src => src.BulkOrder!.CheckedOn)
                ).ForMember(
                   dto => dto.FinishedBy,
                   domain => domain.MapFrom(src => src.BulkOrder!.FinishedBy)
                ).ForMember(
                   dto => dto.FinishedByName,
                   domain => domain.MapFrom(src => src.BulkOrder!.FinishedByName)
                ).ForMember(
                   dto => dto.FinishedOn,
                   domain => domain.MapFrom(src => src.BulkOrder!.FinishedOn)
                ).ForMember(
                   dto => dto.Contract,
                   domain => domain.MapFrom(src => src.BulkOrder!.Contract)
                ).ForMember(
                   dto => dto.Customer,
                   domain => domain.MapFrom(src => src.BulkOrder!.Customer)
                ).ForMember(
                   dto => dto.Supplier,
                   domain => domain.MapFrom(src => src.Supplier)
                ).ForMember(
                   dto => dto.Warehouse,
                   domain => domain.MapFrom(src => src.Warehouse)
                ).ForMember(
                   dto => dto.Contract_Lens,
                   domain => domain.MapFrom(src => src.Contract_Lens)
                ).ForMember(
                   dto => dto.Qty,
                   domain => domain.MapFrom((src, dto) =>
                   {
                       if (src.BulkOrder != null)
                       {
                           if (src.BulkOrder.Type == BulkOrderTypeEnum.Product.ToString())
                               return src.BulkOrderDetail_LensDetails.Sum(x => x.Qty);
                           else if (src.BulkOrder.Type == BulkOrderTypeEnum.MonofocalMaterial.ToString())
                               return src.BulkOrderDetail_MonofocalMaterialDetails.Sum(x => x.Qty);
                           else if (src.BulkOrder.Type == BulkOrderTypeEnum.BifocalMaterial.ToString())
                               return src.BulkOrderDetail_BifocalMaterialDetails.Sum(x => x.Qty);
                       }
                       return 0;

                   })
                ).ForMember(
                   dto => dto.LeaveStockQty,
                   domain => domain.MapFrom(src => src.BulkOrderDetail_LensDetails.Sum(x => x.LeaveStockQty))
                );

            CreateMap<BulkOrderDetail_LensDetail, LensDetailResponse>();

            CreateMap<BulkOrderDetail_MonofocalMaterialDetail, BulkOrderDetail_MonofocalMaterialDetailResponse>();

            CreateMap<BulkOrderDetail_BifocalMaterialDetail, BulkOrderDetail_BifocalMaterialDetailResponse>();

            #endregion

            #region Attachment
            CreateMap<Attachment, AttachmentResponse>();
            //.ForMember(
            //   dto => dto.Url,
            //   domain => domain.MapFrom((src, dto) =>
            //   {
            //       return ConfigurationUtils.GetSection("Authority");
            //   })
            //); 
            #endregion
        }
    }
}
